﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using Entity;
using Business;
using System.Web.UI;

namespace NetFlow
{
    public partial class DurationAnalysis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string filePath = Session["FilePath"].ToString(); // 替换为实际文件路径
                AnalysisBusiness analysisBusiness = new AnalysisBusiness();
                List<NetFlowEntity> netFlowList = analysisBusiness.ParseNetFlowData(filePath);

                // 对持续时间数据进行区间划分
                var durationBuckets = DivideIntoBuckets(netFlowList);

                // 序列化数据
                string jsonData = new JavaScriptSerializer().Serialize(durationBuckets);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ChartData", $"var durationBuckets = {jsonData};", true);

                // 序列化原始数据
                string netFlowDataJson = new JavaScriptSerializer().Serialize(netFlowList);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "NetFlowData", $"var netFlowData = {netFlowDataJson};", true);
            }
        }

        private Dictionary<string, int> DivideIntoBuckets(List<NetFlowEntity> netFlowList)
        {
            var durationBuckets = new Dictionary<string, int>
            {
                { "0-10", 0 },
                { "11-30", 0 },
                { "31-60", 0 },
                { "61-100", 0 },
                { "100+", 0 }
            };

            foreach (var item in netFlowList)
            {
                double duration = item.FlowDuration;
                if (duration <= 10)
                {
                    durationBuckets["0-10"]++;
                }
                else if (duration <= 30)
                {
                    durationBuckets["11-30"]++;
                }
                else if (duration <= 60)
                {
                    durationBuckets["31-60"]++;
                }
                else if (duration <= 100)
                {
                    durationBuckets["61-100"]++;
                }
                else
                {
                    durationBuckets["100+"]++;
                }
            }

            return durationBuckets;
        }
    }
}